//
//  ZQStatusPhotosView.m
//  柳州微博
//
//  Created by Apple_Lzzy45 on 16/11/1.
//  Copyright © 2016年 Apple_Lzzy40. All rights reserved.
//

#import "ZQStatusPhotosView.h"
#import "ZQPicture.h"

#import "ZQStatusPhotoView.h"
@implementation ZQStatusPhotosView
//单张图片的宽高
#define ZQStatusPhotoWH  80
//边距
#define ZQStatusMargin  5 
#define ZQStatusPhotoMaxCols(count) ((count  == 4)?2:3) //最大列数
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
//    if (self) {
//        self.backgroundColor =[UIColor redColor];
//    }
    return self;
}
-(void)setPhotos:(NSArray *)photos
{
    _photos =photos;
    NSInteger photosCount =photos.count;
    while ((self.subviews.count < photosCount)) {
        ZQStatusPhotoView *photoView =[[ZQStatusPhotoView alloc]init];
        [self addSubview:photoView];
    }
    //遍历所有图片控件，设置图片
    for (int i=0; i<self.subviews.count; i++) {
        ZQStatusPhotoView *photoView =self.subviews[i];
        if (i< photosCount) {
            
           ZQPicture *photo =photos[i];
            photoView.photo =photo;
            photoView.hidden = NO;
        }else
        {
            photoView.hidden = YES;
        }
    }
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    NSInteger photosCount =self.photos.count;
//    if (photosCount  == 4) {
//        maxCols =2;
//    }
//    else
//    {
//        maxCols = 3;
//    
//    }

    for (int i =0; i<photosCount; i++) {
        ZQStatusPhotosView *photoView =self.subviews[i];
        int maxCols = ZQStatusPhotoMaxCols(photosCount);
        
        int col =i%maxCols;
        photoView.x=col*(ZQStatusPhotoWH+ZQStatusMargin)+ZQStatusMargin;
        
        int row =i/maxCols;
        photoView.y=row*(ZQStatusPhotoWH+ZQStatusMargin)+ZQStatusMargin;
        photoView.width=ZQStatusPhotoWH;
        photoView.height=ZQStatusPhotoWH;
        
    }
    

 
}
/**
 *  根据图片个数计算view的size
 *
 *
 */
+(CGSize)photosviewSizeWithCount:(NSInteger)count
{
    
    NSInteger rows=0;  //行
    int maxCols = ZQStatusPhotoMaxCols(count);
    
    rows =(count+(maxCols - 1))/maxCols;
    NSInteger cols=0;  //列
   
    cols  = (count>=3) ? 3 : count;
    if (count==4) {
        rows =2;
        cols =2;
    }
    //1列两边   2列3边   3列4边
    CGFloat photosW =cols *ZQStatusPhotoWH +(cols + 1) *ZQStatusMargin;
    CGFloat photosH = rows *ZQStatusPhotoWH +(rows + 1)*ZQStatusMargin;
    
    //1张1行1列   2张1行2列   3张1行3列  4张2行2列  5张2行2列
    
    return  CGSizeMake(photosW, photosH);
}

@end
